package com.zd.lib.glide;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.GlideModule;
import com.zd.lib.utils.FileUtils;

import java.io.InputStream;

/**
 * @author zoudong
 */
public class GildeConfiguration implements GlideModule {
    public static final int CACHESIZE = 100 * 1024 * 1024;

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        MemorySizeCalculator calculator = new MemorySizeCalculator(context);
        int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
        int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
        int customMemoryCacheSize = (int) (0.5 * defaultMemoryCacheSize);
        int customBitmapPoolSize = (int) (0.5 * defaultBitmapPoolSize);
//        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
        builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
        final String downloadDirectoryPath = FileUtils.getCacheDir(context) + "gilde";
        builder.setDiskCache(new DiskLruCacheFactory(downloadDirectoryPath, CACHESIZE));
    }
//
    @Override
    public void registerComponents(Context context, Glide glide) {
        //1.glide 修改默认加载 使用OKHttp 2。cachekey 修改为 图片路径
        glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
    }

}
